
package com.scs.application.modules.base.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scs.application.core.exceptions.BusinessException;
import com.scs.application.core.model.CommonResult;
import com.scs.application.core.model.PageResult;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.mvc.controller.ImAndExAbleController;
import com.scs.application.core.utils.BeanValidators;
import com.scs.application.core.utils.UserUtils;
import com.scs.application.modules.base.entity.Hospital;
import com.scs.application.modules.base.entity.HospitalWarehouse;
import com.scs.application.modules.base.request.HospitalWarehouseQueryRequest;
import com.scs.application.modules.base.service.HospitalService;
import com.scs.application.modules.base.service.HospitalWarehouseService;
import lombok.AllArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 医院收货地点 controller
 */
@AllArgsConstructor
@RestController
@RequestMapping("/base/hospital_warehouses")
public class HospitalWarehouseController extends CrudController<HospitalWarehouse, String, HospitalWarehouseService> implements ImAndExAbleController<HospitalWarehouseQueryRequest> {
    private final HospitalService hospitalService;

    @GetMapping("/page")
    public PageResult page(HospitalWarehouseQueryRequest request) {
        IPage page = service.page(request);
        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    @GetMapping("/list")
    public CommonResult list(HospitalWarehouseQueryRequest request) {
        List<HospitalWarehouse> list = service.list(request);
        return CommonResult.ok("查询成功", list);
    }

    /**
     * 新增仓库信息
     *
     * @param entity
     * @return
     */
    @PostMapping
    public CommonResult add(@RequestBody @Valid HospitalWarehouse entity) {
        entity.setHospitalId(UserUtils.getHospitalId());
        boolean bSave = service.add(entity);
        fillData(entity);
        return bSave ? CommonResult.ok("新增成功", entity) : CommonResult.error("仓库名称或仓库代码不可重复");
    }

    private void fillData(HospitalWarehouse entity) {
        Hospital hospital = hospitalService.getById(entity.getHospitalId());
        BusinessException.throwErrorIf(hospital == null, "未找到所属医院");
        entity.setHospitalName(hospital.getName());
    }

    /**
     * 更新仓库信息
     *
     * @param entity
     * @return
     */
    @Override
    @PutMapping
    public CommonResult update(@RequestBody @Valid HospitalWarehouse entity) {
        entity.setHospitalId(UserUtils.getHospitalId());
        boolean bUpdate = service.update(entity);
        fillData(entity);
        return bUpdate ? CommonResult.ok("保存成功", entity) : CommonResult.error("仓库名称或仓库代码不可重复");
    }


    @PutMapping("/batch_save")
    public CommonResult batchSave(@RequestBody @Valid List<HospitalWarehouse> children) {
        Assert.notEmpty(children, "至少有一条");
        BeanValidators.validateListWithException(validator, children);

        boolean bSuccess = service.saveOrUpdateBatch(children);
        return bSuccess ? CommonResult.ok("保存成功") : CommonResult.error("保存失败");
    }


    /***
     * @param entity
     * @Description:  通用保存、更新
     */
    @PostMapping("/saveOrUpdateCustom")
    public CommonResult saveOrUpdateCustom(@Valid @RequestBody HospitalWarehouse entity) {
        if (entity.isNewRecord()) {
            return  add(entity);
        }
        return  update(entity);
    }

}
